package org.jsrml.core.purchaser.service;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.jsrml.common.persistence.BaseDao;
import org.jsrml.core.purchaser.command.RemovePositionsCo;
import org.jsrml.core.purchaser.command.SavePositionsCo;
import org.jsrml.core.purchaser.entity.Department;
import org.jsrml.core.purchaser.entity.Positions;
import org.jsrml.core.purchaser.exception.PurchaserException;
import org.jsrml.core.purchaser.qo.PositionsQO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class PositionsService extends BaseDao<Positions, PositionsQO> {

	@Autowired
	private DepartmentService departmentService;
	
	public Positions createPositions(SavePositionsCo command){
		Department department = null;
		if(StringUtils.isNotBlank(command.getDepartmentId())){
			department = departmentService.get(command.getDepartmentId());
		}
		Positions positions = new Positions();
		positions.create(command, department);
		save(positions);
		return positions;
	}
	
	public Positions modifyPositions(SavePositionsCo command) throws PurchaserException{
		
		if(StringUtils.isBlank(command.getPositionId())){
			throw new PurchaserException(
					PurchaserException.NECESSARY_PARAM_NULL, "缺少必需的参数!");
		}
		
		Positions positions = get(command.getPositionId());
		
		if (positions == null) {
			throw new PurchaserException(
					PurchaserException.POSITIONS_NOT_FOUND, "职称没有找到!");
		}
		
		Department department = null;
		if(StringUtils.isNotBlank(command.getDepartmentId())){
			department = departmentService.get(command.getDepartmentId());
		}
		positions.modify(command, department);
		return positions;
	}
	
	public void removePositions(RemovePositionsCo command) throws PurchaserException {
		
		if(StringUtils.isBlank(command.getPositionsId())){
			throw new PurchaserException(
					PurchaserException.NECESSARY_PARAM_NULL, "缺少必需的参数!");
		}
		
		Positions positions = get(command.getPositionsId());
		
		if (positions == null) {
			throw new PurchaserException(
					PurchaserException.POSITIONS_NOT_FOUND, "职称没有找到!");
		}
		
		positions.remove();
	}
	
	@Transactional(rollbackFor = PurchaserException.class)
	public void plRemovePositions(RemovePositionsCo command) throws PurchaserException{
		if(command.getPositionsIds()==null || command.getPositionsIds().size()==0){
			throw new PurchaserException(
					PurchaserException.NECESSARY_PARAM_NULL, "缺少必需的参数!");
		}
		
		for(String positionsId : command.getPositionsIds()){
			Positions positions = get(positionsId);
			if (positions == null) {
				throw new PurchaserException(
						PurchaserException.POSITIONS_NOT_FOUND, "职务没有找到!");
			}
			positions.getStatus().setRemove(true);
			save(positions);
		}
	}

	@Override
	protected Criteria buildCriteria(Criteria criteria, PositionsQO qo) {
		return criteria;
	}

	@Override
	protected Class<Positions> getEntityClass() {
		return Positions.class;
	}
}
